<?php

/**
 * page class. Provide utilities functions related to page table in database.
 * <u>Note:</u> all $db param in first parameter is ezSQL_mysql object.
 * See ezSQL documentation for more information.
 */
class FIN_pageBean 
{

    function FIN_pageBean() 
    {
    }
    
    /**
	 * Get page by Id.
	 * @param $id the id of page will be deleted.
	 * @return page object, field name same as table in database.
	 */
	function getAllPageByType($db,$type)
    {
    	$page = null;
    	$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT p.page_id,pl.page_name FROM ".TBL_PAGE." p ".
						"LEFT JOIN ".TBL_PAGE_LANG." pl ON p.page_id = pl.page_id AND pl.lang_id = '$locale' ".
						"WHERE p.page_type=" . $type." AND p.page_parent 	= 0");
    	$page = $db->get_results($sql);
    	return $page;
    }	
	
	function countAllPageChild($db,$type,$pageparent){
    	$page = null;
    	$locale = $_SESSION["LC_ALL"];
    	$sql = sprintf("SELECT COUNT(p.page_id) as total FROM ".TBL_PAGE." p ".
						"LEFT JOIN ".TBL_PAGE_LANG." pl ON p.page_id = pl.page_id AND pl.lang_id = '$locale' ".
						"WHERE p.page_type=" . $type." AND p.page_parent = $pageparent");
    	$page = $db->get_row($sql);
    	return $page->total;
    }
	
	function getAllPageChildShare($db,$type,$pageparent,$share,$path){
    	$page = null;
    	$locale = $_SESSION["LC_ALL"];
		$page = isset ( $_GET[$path] ) ? intval ( $_GET[$path] ) : 1;
		$rows_per_page= $share;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
    	$sql = sprintf("SELECT p.*,pl.page_name,pl.short_desciption FROM ".TBL_PAGE." p ".
						"LEFT JOIN ".TBL_PAGE_LANG." pl ON p.page_id = pl.page_id AND pl.lang_id = '$locale' ".
						"WHERE p.page_type=" . $type." AND p.page_parent = $pageparent LIMIT $page_start, $page_end");
    	$page = $db->get_results($sql);
    	return $page;
    }
	/**
	 * Get page by Id.
	 * @param $id the id of page will be deleted.
	 * @return page object, field name same as table in database.
	 */
	function getPageById($db,$locale,$type, $id)
    {
    	$page = null;
		if($id!='')
			$cond = " AND p.page_id=" . $id.""	;
		if($type!='')
			$cond1 = " AND p.page_type=" . $type.""	;	
    	$sql = sprintf("SELECT * FROM ".TBL_PAGE." p ".
						"LEFT JOIN ".TBL_PAGE_LANG." pl ON p.page_id = pl.page_id AND pl.lang_id = '$locale' ".
						"WHERE pl.lang_id = '$locale' ".$cond1.$cond." LIMIT 0,1");
    	$page = $db->get_row($sql);
    	return $page;
    }	
	
	function CheckViewIntroPane($db,$id)
    {	
    	$sql = sprintf("SELECT c.intro_pane FROM ".TBL_CATEGORY." c ".
						"WHERE c.cat_ID=" . $id);
    	$page = $db->get_row($sql);
    	return $page;
    }	
	
	function getPageByLink($db,$locale,$type, $id)
    {
    	$page = null;
		$cond='';
		$cond1='';
		if($id!='')
			$cond = " AND d.category_id=" . $id."";
		if($type!='')
			$cond1 = " AND p.page_type=" . $type.""	;	
    	$sql = sprintf("SELECT pl.*,c.intro_pane FROM ".TBL_PAGE2CAT." d ".
					   "LEFT JOIN ".TBL_CATEGORY." c ON c.cat_ID = d.category_id ".	
					   "LEFT JOIN ".TBL_PAGE." p ON p.page_id = d.page_id ".	
						"LEFT JOIN ".TBL_PAGE_LANG." pl ON p.page_id = pl.page_id AND pl.lang_id = '$locale' ".
						"WHERE pl.lang_id = '$locale' ".$cond1.$cond." LIMIT 0,1");
    	$page = $db->get_row($sql);
    	return $page;
    }	
	function getTypeIntroPane($db,$lang_id)
    {
    	$sql = sprintf("SELECT pl.page_name,pl.short_desciption,c.cat_ID,c.cat_parent,cl.cat_name FROM ".TBL_INTRO_PANE." i ".
					   "LEFT JOIN ".TBL_PAGE2CAT." d ON d.page_id = i.page_id ".
					    "LEFT JOIN ".TBL_CATEGORY." c ON c.cat_ID = d.category_id ".
						" LEFT JOIN ".TBL_CATEGORY_LANG." cl ON cl.cat_id = c.cat_ID AND cl.lang_id = '$lang_id'".
					   "LEFT JOIN ".TBL_PAGE_LANG." pl ON pl.page_id = i.page_id AND pl.lang_id = '$lang_id'"
					   );
    	$intro = $db->get_results($sql);
    	return $intro;
    }
}
?>